package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.SysConfigDao;
import cc.linker.steplink.crm.service.ISysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;

@Service
public class SysConfigImpl implements ISysConfig {


    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    public String getSysConfByName(String orgId,String name){
        return sysConfigDao.getSysConfByName(orgId,name);
    };

    @Override
    public Boolean addSysConf(Map param){
        return sysConfigDao.addSysConf(param);
    };
    @Override
    public Boolean updateSysConf(Map param){
        return sysConfigDao.updateSysConf(param);
    };

    @Override
    public List<Map<String,String>> getWxTokenList(){
        return sysConfigDao.getWxTokenList();
    }

    @Override
    public void saveRobotDynamic(Map param){
        if("market".equals(param.get("robotType")) && "1".equals(param.get("dynamicType"))){
            String marketRobotSwitch = sysConfigDao.getSysConfByName(param.get("orgId").toString(), "marketRobotSwitch");
            if(null!=marketRobotSwitch && "1".equals(marketRobotSwitch)){
                Integer integer = sysConfigDao.countDayRecord(param);
                if(integer>0){
                    sysConfigDao.editRobotDynamic(param);
                }else{
                    sysConfigDao.saveRobotDynamic(param);
                }
            }
        }
        if("client".equals(param.get("robotType")) && "1".equals(param.get("dynamicType"))){
            String clientRobotSwitch = sysConfigDao.getSysConfByName(param.get("orgId").toString(), "clientRobotSwitch");
            if(null!=clientRobotSwitch && "1".equals(clientRobotSwitch)){
                Integer integer = sysConfigDao.countDayRecord(param);
                if(integer>0){
                    sysConfigDao.editRobotDynamic(param);
                }else{
                    sysConfigDao.saveRobotDynamic(param);
                }
            }
        }
        if("3".equals(param.get("dynamicType")) || "4".equals(param.get("dynamicType"))){
            sysConfigDao.saveRobotDynamic(param);
        }
    }

    @Override
    public Integer getWxCountByOrgId(String orgId){
        return sysConfigDao.getWxCountByOrgId(orgId);
    }
}
